package com.jrapider;

import com.jrapider.compent.dict.DictTemplateDirectiveModel;
import freemarker.template.SimpleScalar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;

/**
 * @Auther: hxs
 * @Date: 2019/1/1
 * @Description:
 */
@Configuration
public class FreemarkerConfig {
    @Autowired
    protected FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private DictTemplateDirectiveModel dictTemplateDirectiveModel;

    @PostConstruct
    public void init() {
        freemarker.template.Configuration configuration = freeMarkerConfigurer.getConfiguration();
        configuration.setSharedVariable("dict", dictTemplateDirectiveModel);
    }

    public static String transString(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof SimpleScalar) {
            return ((SimpleScalar) value).getAsString();
        } else {
            return null;
        }
    }
}
